<?php

function emoji_utf8_bytes($cp){
	$result 	= false;
	if(is_array($cp)){
		foreach($cp as $val){
			$result	.= emoji_utf8_bytes($val);
		}
		return $result;
	}else{
		if ($cp > 0x10000){
			# 4 bytes
			return	chr(0xF0 | (($cp & 0x1C0000) >> 18)).
				chr(0x80 | (($cp & 0x3F000) >> 12)).
				chr(0x80 | (($cp & 0xFC0) >> 6)).
				chr(0x80 | ($cp & 0x3F));
		}else if ($cp > 0x800){
			# 3 bytes
			return	chr(0xE0 | (($cp & 0xF000) >> 12)).
				chr(0x80 | (($cp & 0xFC0) >> 6)).
				chr(0x80 | ($cp & 0x3F));
		}else if ($cp > 0x80){
			# 2 bytes
			return	chr(0xC0 | (($cp & 0x7C0) >> 6)).
				chr(0x80 | ($cp & 0x3F));
		}else{
			# 1 byte
			return chr($cp);
		}
	}
}
function unicode2utf8($str) { // unicode编码转化，用于显示emoji表情
    $str = '{"result_str":"' . $str . '"}'; // 组合成json格式
    $strarray = json_decode ( $str, true ); // json转换为数组，利用 JSON 对 \uXXXX 的支持来把转义符恢复为 Unicode 字符
    return $strarray ['result_str'];
}

/**
 * 获取在线人数
 * @return 	$count
 */
function get_online_count(){
	return rand(4000, 5000);
}

/**
 * blog:http://www.zhaokeli.com
 * 处理圆角图片
 * @param  string  $imgpath 源图片路径
 * @param  integer $radius  圆角半径长度默认为15,处理成圆型
 * @return [type]           [description]
 */
function gd_radius_img($imgpath = './t.png', $radius = 15) {
	$ext     = pathinfo($imgpath);
	$src_img = null;
	switch ($ext['extension']) {
		case 'jpg':
			$src_img = imagecreatefromjpeg($imgpath);
			break;
		case 'png':
			$src_img = imagecreatefrompng($imgpath);
			break;
		default:
			$src_img = imagecreatefrompng($imgpath);
			break;
	}
	$wh = getimagesize($imgpath);
	$w  = $wh[0];
	$h  = $wh[1];
	// $radius = $radius == 0 ? (min($w, $h) / 2) : $radius;
	$img = imagecreatetruecolor($w, $h);
	//这一句一定要有
	imagesavealpha($img, true);
	//拾取一个完全透明的颜色,最后一个参数127为全透明
	$bg = imagecolorallocatealpha($img, 255, 255, 255, 127);
	imagefill($img, 0, 0, $bg);
	$r = $radius; //圆 角半径
	for ($x = 0; $x < $w; $x++) {
		for ($y = 0; $y < $h; $y++) {
			$rgbColor = imagecolorat($src_img, $x, $y);
			if (($x >= $radius && $x <= ($w - $radius)) || ($y >= $radius && $y <= ($h - $radius))) {
				//不在四角的范围内,直接画
				imagesetpixel($img, $x, $y, $rgbColor);
			} else {
				//在四角的范围内选择画
				//上左
				$y_x = $r; //圆心X坐标
				$y_y = $r; //圆心Y坐标
				if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($r * $r))) {
					imagesetpixel($img, $x, $y, $rgbColor);
				}
				//上右
				$y_x = $w - $r; //圆心X坐标
				$y_y = $r; //圆心Y坐标
				if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($r * $r))) {
					imagesetpixel($img, $x, $y, $rgbColor);
				}
				//下左
				$y_x = $r; //圆心X坐标
				$y_y = $h - $r; //圆心Y坐标
				if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($r * $r))) {
					imagesetpixel($img, $x, $y, $rgbColor);
				}
				//下右
				$y_x = $w - $r; //圆心X坐标
				$y_y = $h - $r; //圆心Y坐标
				if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($r * $r))) {
					imagesetpixel($img, $x, $y, $rgbColor);
				}
			}
		}
	}
	return $img;
}
